package com.linewell.dao;

import com.linewell.entity.Article;
import com.linewell.entity.Letter;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface LetterMapper{

    @Insert("insert into letter values(#{letterId},#{writerUser.userId},#{readerUser.userId},#{content},#{sendTime},#{isRead},#{parentId})")
    int insertLetter(Letter letter);
    @ResultMap("com.linewell.dao.LetterMapper.letter")
    @Select("select l.*,user_id,nick_name,picture from letter l,userinfo where l.isRead = 0 and l.reader = ${id} and l.writer = user_id order by(l.sendTime) Desc; ")
    List<Letter> selectLetter(@Param("id") Long userId);

    @Update("update letter set isRead = 1 where letterId = #{letterId}")
    boolean updateLetterIsRead(@Param("letterId")String letterId);

    @ResultMap("com.linewell.dao.LetterMapper.letter")
    @Select("select l.*,user_id,nick_name,picture " +
            "from letter l,userinfo " +
            "where l.isRead = 0 and l.parentId = #{letterId} and l.writer = user_id order by(l.sendTime) Desc; ")
    List<Letter> getLetterById(@Param("letterId")String letterId);

    @Select("select writer from letter where letterId=#{letterId}")
    String getUserByletterId(@Param("letterId")String letterId);

    @Insert("insert into letter values(#{letterId},#{writerUser.userId},#{readerUser.userId},#{content},#{sendTime},#{isRead},null)")
    boolean opinion(Letter letter);

    @ResultMap("com.linewell.dao.LetterMapper.systemMsg")
    @Select("select letterId,l.content,sendTime,isRead,u.user_id,nick_name,picture,article_id,title " +
            "from article a,letter l,userinfo u " +
            "where u.user_id = ${userId} " +
            "and u.user_id = a.user_id " +
            "and a.article_id = l.reader ")
    List<Letter> selectSystemLetter(@Param("userId")Long userId);


    @Select("select a.* from article a, letter l " +
            "where l.letterId = #{letterId} " +
            "AND l.reader = a.article_id")
    Article getArticleByLetterId(@Param("letterId")String letterId);

    @Delete("delete from letter where reader = ${articleId}")
    void deleteLetterByArticle(@Param("articleId")Long articleId);
}
